// pages/showCases/showCases.js
var app = getApp()

var util = require("../../utils/util.js")
var getAuthCase_url = 'https://www.hkzhaoscut.top/manage/getAllAuthCase'
var auth_url ="https://www.hkzhaoscut.top/manage/authCase/"
var pageNum = 2
var pageSize = 5
var degreeSelection = ['无症状', '轻症', '重症', '治愈', '死亡']

// 获取初始的病例数据
var getInitCases = function (that, token) {
  var managerToken = token
  // console.log("managertoken为：",managerToken)
  wx.request({
    url: getAuthCase_url,
    header: {
      'Authorization': managerToken
    },
    data: {
      pageNum: 1,
      pageSize: pageSize
    },
    success: res => {
      // console.log('获取初始数据成功', res.data)
      if (res.data.code == 200) {
        console.log('获取初始数据成功', res.data)
        var list = res.data.data
        var tmplist = []
        if (list.length != 0) {
          list.forEach(function (item, index) {
            tmplist[index] = {}
            var date = new Date(item.time)
            tmplist[index].reportDate = util.formatTimeWithoutHour(date)
            tmplist[index].illDate = util.formatTimeWithoutHour(date)
            tmplist[index].pSex = item.gender
            tmplist[index].patientName = item.name
            tmplist[index].pAddress = util.formatAddress(item.locate)
            tmplist[index].pPhone = item.telephone
            tmplist[index].condition = item.level
            tmplist[index].conditionId = degreeSelection.indexOf(item.level)
            tmplist[index].illness = item.diseaseType
            tmplist[index].pAge = item.age
            tmplist[index].remarks = item.note
            tmplist[index].managerTokenid = item.managerTokenid
            tmplist[index].caseid = item.caseId
          })
          var curCases = tmplist
          that.setData({
            case_list: curCases
          })
        }

      }

    }
  })

}
// 获取更多的病例数据
var getMoreCases = function (that) {
  var managerToken = that.data.managerToken
  wx.request({
    url: getAuthCase_url,
    header: {
      'Authorization': managerToken,
    },
    data: {
      pageNum: pageNum,
      pageSize: pageSize
    },
    success: res => {
      if (res.data.code == 200) {
        // console.log('获取初始数据成功', res.data)
        var list = res.data.data
        var tmplist = []
        if (list.length != 0) {
          pageNum++
          list.forEach(function (item, index) {
            tmplist[index] = {}
            var date = new Date(item.time)
            tmplist[index].reportDate = util.formatTimeWithoutHour(date)
            tmplist[index].illDate = util.formatTimeWithoutHour(date)
            tmplist[index].pSex = item.gender
            tmplist[index].patientName = item.name
            tmplist[index].pAddress = util.formatAddress(item.locate)
            tmplist[index].pPhone = item.telephone
            tmplist[index].condition = item.level
            tmplist[index].conditionId = degreeSelection.indexOf(item.level)
            tmplist[index].illness = item.diseaseType
            tmplist[index].pAge = item.age
            tmplist[index].remarks = item.note
            tmplist[index].managerTokenid = item.managerTokenid
            tmplist[index].caseid = item.caseId
          })
          var originCases = that.data.case_list
          var curCases = originCases.concat(tmplist)
          that.setData({
            case_list: curCases
          })
        }

      }

    }
  })

}
Page({
  data: {
    case_list: [], //案例数组
    timeSelection: ['最晚上报', '最早上报'],
    timeSelected: true, //是否点击了按上报时间排序
    byTime: 0, //0代表按最晚上报排序 1代表按最早上报排序
    maskFlag: true, //遮罩层flag
    degreeSelection: ['无症状', '轻症', '重症', '治愈', '死亡'],
    degreeSelected: true, //是否点击的按紧急程度排序的按钮
    byDegree: 0, //0-4分表表示不同程度
    managerToken: app.globalData.managerToken,
    maskFlag2:true,//修改详情遮罩层
    curCase:[],//当前遮罩层显示的数据
  },
  // 提交审核函数
  authSubmit(e) {
    var target = e.currentTarget.dataset.pass
    var title = ""
    if (target == 1) {
      title = "确定审核通过吗？"
    } else {
      title = "审核不通过！"
    }
    wx.showModal({
      title: '提交审核',
      content: title,
      success: function (res) {
        if (res.confirm) {
          console.log(e.currentTarget.dataset,app.globalData.managerToken)
          var caseid = e.currentTarget.dataset.caseid
          
          wx.request({
            url: auth_url,
            method:"POST",
            header: {
              'Authorization': app.globalData.managerToken,
              'Content-Type':"application/x-www-form-urlencoded"
            },
            
            data: {
              case_token_id: caseid,
              target: target
            },
            success(res) {
              console.log(res)
            }
          })
        }
      }
    })
  },
  // 展开病例修改详情
  toAuth(e) {
    var curCase = e.currentTarget.dataset.cur
    console.log(curCase)
    this.setData({
      maskFlag2:false,
      curCase: curCase
    })
  },
  //遮罩2隐藏
  hideMask(){
    this.setData({
      maskFlag2: true
    })
  },

  // 显示按上报时间排序的顶部栏
  forTime(e) {
    var timeSelected = !this.data.timeSelected
    this.setData({
      timeSelected: timeSelected
    })
    this.showMask()
  },
  // 显示按状态排序的顶部栏
  forDegree(e) {
    var degreeSelected = !this.data.degreeSelected
    this.setData({
      degreeSelected: degreeSelected
    })
    this.showMask()
  },
  // 状态的选择项
  selectDegree(e) {
    var that = this
    // console.log(e)
    var selected = e.currentTarget.id
    var condition = that.data.degreeSelection[selected]
    that.setData({
      byDegree: selected
    })
    // console.log(condition)
    var list = that.data.case_list.sort(that.compareDegree('condition', condition));
    that.setData({
      case_list: list
    })
    that.forDegree()
  },
  // 比较状态
  compareDegree: function (property, condition) {
    return function (a, b) {
      if (a[property] == condition) {
        var value1 = 1
      } else {
        var value1 = 0
      }
      if (b[property] == condition) {
        var value2 = 1
      } else {
        var value2 = 0
      }
      // console.log(value1,value2)
      return value2 - value1;

    }
  },
  // 上报时间的选择项
  selectTime(e) {
    // console.log(e)
    var that = this
    var selected = e.currentTarget.id
    that.setData({
      byTime: selected
    })
    var list = that.data.case_list.sort(that.compareTime('reportDate', selected));
    that.setData({
      case_list: list
    })
    that.forTime()
    // console.log(list)
  },
  // 比较日期
  compareTime: function (property, way) {
    return function (a, b) {
      var value1 = Date.parse(a[property]);
      var value2 = Date.parse(b[property]);
      if (way == 0) {
        return value2 - value1;
      } else {
        return value1 - value2;
      }
    }
  },
  // 展示遮罩层
  showMask() {
    var maskFlag = !this.data.maskFlag
    this.setData({
      maskFlag: maskFlag
    })
  },
  //触底函数
  onReachBottom: function () {
    console.log("到底部了")
    getMoreCases(this)
  },

  // onLoad函数
  onLoad(e) {
    var userInfo = wx.getStorageSync('userInfo')
    // console.log(userInfo)
    if (app.globalData.userType == 'manager' && app.globalData.loginStauts) {
      getInitCases(this, userInfo.managerToken)
    }
    this.setData({
      managerToken: userInfo.managerToken
    })


  },
  // onShow函数
  onShow(e) {
    // 这里向后台请求数据

  }
})